package com.zmn.oms.admin.controller.work.modify;

import com.zmn.common.utils.encode.EncodeUtil;
import com.zmn.oms.admin.controller.work.WorkBaseController;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.dto.work.masterwork.ServiceCompleteDIO;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.zmn.business.interfaces.work.autoflow.WorkFlowContextBService;
import com.zmn.oms.zmn.business.interfaces.work.masterwork.ZsMasterWorkBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author sunlife
 * @date:2020/9/2 6:30 下午
 * description:
 */
@Controller
@RequestMapping("/work/modify")
public class WorkServiceCompleteController extends WorkBaseController {


    @Autowired
    ZsMasterWorkBService zsMasterWorkBService;
    @Autowired
    WorkFlowContextBService workFlowContextBService;

    /**
     * 完成
     */
    @RequestMapping(value = "/serviceComplete")
    public ModelAndView serviceComplete(Long orderId, Long workId, Long operatorId) {
        ModelAndView mv = new ModelAndView("work/modify/servicecomplete");
        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, workId);

        mv.addObject("orderWork", orderWork);
        mv.addObject("needCompleteCode", zsMasterWorkBService.needCompleteCode(orderId, workId));
        String completeCode = EncodeUtil.getFeatureCode(orderWork.getWorkId(), 4);
        mv.addObject("completeCode", completeCode);
        return mv;
    }

    /**
     * 完成
     */
    @RequestMapping(value = "/save/serviceComplete")
    @ResponseBody
    public ResultDTO save(@RequestBody ServiceCompleteDIO dio) throws OmsBaseException {
        zsMasterWorkBService.serviceComplete(dio);

        workFlowContextBService.asyncAutoFlow(dio.getOrderId(), dio.getWorkId());

        return ResultDTO.success();
    }
}
